home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg4.cab / test_strftime.py < prev    next >
Text File  |  2005-11-19  |  6KB  |  159 lines

  1. #! /usr/bin/env python
  2.  
  3. # Sanity checker for time.strftime
  4.  
  5. import time, calendar, sys, os, re
  6. from test.test_support import verbose
  7.  
  8. def main():
  9.     global verbose
  10.     # For C Python, these tests expect C locale, so we try to set that
  11.     # explicitly.  For Jython, Finn says we need to be in the US locale; my
  12.     # understanding is that this is the closest Java gets to C's "C" locale.
  13.     # Jython ought to supply an _locale module which Does The Right Thing, but
  14.     # this is the best we can do given today's state of affairs.
  15.     try:
  16.         import java
  17.         java.util.Locale.setDefault(java.util.Locale.US)
  18.     except ImportError:
  19.         # Can't do this first because it will succeed, even in Jython
  20.         import locale
  21.         locale.setlocale(locale.LC_TIME, 'C')
  22.     now = time.time()
  23.     strftest(now)
  24.     verbose = 0
  25.     # Try a bunch of dates and times,  chosen to vary through time of
  26.     # day and daylight saving time
  27.     for j in range(-5, 5):
  28.         for i in range(25):
  29.             strftest(now + (i + j*100)*23*3603)
  30.  
  31. def escapestr(text, ampm):
  32.     """Escape text to deal with possible locale values that have regex
  33.     syntax while allowing regex syntax used for the comparison."""
  34.     new_text = re.escape(text)
  35.     new_text = new_text.replace(re.escape(ampm), ampm)
  36.     new_text = new_text.replace("\%", "%")
  37.     new_text = new_text.replace("\:", ":")
  38.     new_text = new_text.replace("\?", "?")
  39.     return new_text
  40.  
  41. def strftest(now):
  42.     if verbose:
  43.         print "strftime test for", time.ctime(now)
  44.     nowsecs = str(long(now))[:-1]
  45.     gmt = time.gmtime(now)
  46.     now = time.localtime(now)
  47.  
  48.     if now[3] < 12: ampm='(AM|am)'
  49.     else: ampm='(PM|pm)'
  50.  
  51.     jan1 = time.localtime(time.mktime((now[0], 1, 1) + (0,)*6))
  52.  
  53.     try:
  54.         if now[8]: tz = time.tzname[1]
  55.         else: tz = time.tzname[0]
  56.     except AttributeError:
  57.         tz = ''
  58.  
  59.     if now[3] > 12: clock12 = now[3] - 12
  60.     elif now[3] > 0: clock12 = now[3]
  61.     else: clock12 = 12
  62.  
  63.     # Make sure any characters that could be taken as regex syntax is
  64.     # escaped in escapestr()
  65.     expectations = (
  66.         ('%a', calendar.day_abbr[now[6]], 'abbreviated weekday name'),
  67.         ('%A', calendar.day_name[now[6]], 'full weekday name'),
  68.         ('%b', calendar.month_abbr[now[1]], 'abbreviated month name'),
  69.         ('%B', calendar.month_name[now[1]], 'full month name'),
  70.         # %c see below
  71.         ('%d', '%02d' % now[2], 'day of month as number (00-31)'),
  72.         ('%H', '%02d' % now[3], 'hour (00-23)'),
  73.         ('%I', '%02d' % clock12, 'hour (01-12)'),
  74.         ('%j', '%03d' % now[7], 'julian day (001-366)'),
  75.         ('%m', '%02d' % now[1], 'month as number (01-12)'),
  76.         ('%M', '%02d' % now[4], 'minute, (00-59)'),
  77.         ('%p', ampm, 'AM or PM as appropriate'),
  78.         ('%S', '%02d' % now[5], 'seconds of current time (00-60)'),
  79.         ('%U', '%02d' % ((now[7] + jan1[6])//7),
  80.          'week number of the year (Sun 1st)'),
  81.         ('%w', '0?%d' % ((1+now[6]) % 7), 'weekday as a number (Sun 1st)'),
  82.         ('%W', '%02d' % ((now[7] + (jan1[6] - 1)%7)//7),
  83.          'week number of the year (Mon 1st)'),
  84.         # %x see below
  85.         ('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
  86.         ('%y', '%02d' % (now[0]%100), 'year without century'),
  87.         ('%Y', '%d' % now[0], 'year with century'),
  88.         # %Z see below
  89.         ('%%', '%', 'single percent sign'),
  90.         )
  91.  
  92.     nonstandard_expectations = (
  93.         # These are standard but don't have predictable output
  94.         ('%c', fixasctime(time.asctime(now)), 'near-asctime() format'),
  95.         ('%x', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)),
  96.          '%m/%d/%y %H:%M:%S'),
  97.         ('%Z', '%s' % tz, 'time zone name'),
  98.  
  99.         # These are some platform specific extensions
  100.         ('%D', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), 'mm/dd/yy'),
  101.         ('%e', '%2d' % now[2], 'day of month as number, blank padded ( 0-31)'),
  102.         ('%h', calendar.month_abbr[now[1]], 'abbreviated month name'),
  103.         ('%k', '%2d' % now[3], 'hour, blank padded ( 0-23)'),
  104.         ('%n', '\n', 'newline character'),
  105.         ('%r', '%02d:%02d:%02d %s' % (clock12, now[4], now[5], ampm),
  106.          '%I:%M:%S %p'),
  107.         ('%R', '%02d:%02d' % (now[3], now[4]), '%H:%M'),
  108.         ('%s', nowsecs, 'seconds since the Epoch in UCT'),
  109.         ('%t', '\t', 'tab character'),
  110.         ('%T', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
  111.         ('%3y', '%03d' % (now[0]%100),
  112.          'year without century rendered using fieldwidth'),
  113.         )
  114.  
  115.     if verbose:
  116.         print "Strftime test, platform: %s, Python version: %s" % \
  117.               (sys.platform, sys.version.split()[0])
  118.  
  119.     for e in expectations:
  120.         try:
  121.             result = time.strftime(e[0], now)
  122.         except ValueError, error:
  123.             print "Standard '%s' format gave error:" % e[0], error
  124.             continue
  125.         if re.match(escapestr(e[1], ampm), result): continue
  126.         if not result or result[0] == '%':
  127.             print "Does not support standard '%s' format (%s)" % (e[0], e[2])
  128.         else:
  129.             print "Conflict for %s (%s):" % (e[0], e[2])
  130.             print "  Expected %s, but got %s" % (e[1], result)
  131.  
  132.     for e in nonstandard_expectations:
  133.         try:
  134.             result = time.strftime(e[0], now)
  135.         except ValueError, result:
  136.             if verbose:
  137.                 print "Error for nonstandard '%s' format (%s): %s" % \
  138.                       (e[0], e[2], str(result))
  139.             continue
  140.         if re.match(escapestr(e[1], ampm), result):
  141.             if verbose:
  142.                 print "Supports nonstandard '%s' format (%s)" % (e[0], e[2])
  143.         elif not result or result[0] == '%':
  144.             if verbose:
  145.                 print "Does not appear to support '%s' format (%s)" % (e[0],
  146.                                                                        e[2])
  147.         else:
  148.             if verbose:
  149.                 print "Conflict for nonstandard '%s' format (%s):" % (e[0],
  150.                                                                       e[2])
  151.                 print "  Expected %s, but got %s" % (e[1], result)
  152.  
  153. def fixasctime(s):
  154.     if s[8] == ' ':
  155.         s = s[:8] + '0' + s[9:]
  156.     return s
  157.  
  158. main()
  159.